package org.zhxy.zhxy.controller.front;



import org.zhxy.zhxy.entity.AppointRecord;
import org.zhxy.zhxy.entity.Classroom;
import org.zhxy.zhxy.entity.dto.ClassRoomSearchDto;
import org.zhxy.zhxy.service.AppointRecordService;
import org.zhxy.zhxy.service.ClassroomService;
import org.springframework.web.bind.annotation.*;
import org.zhxy.zhxy.utils.Result;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 教室表(Classroom)表控制层
 *
 * @author yayong
 * @since 2024-10-19 21:25:48
 */
@RestController
@RequestMapping("/api/classroom")
public class ClassroomController {

/**
 * 服务对象
 */
@Resource
private ClassroomService classroomService;
@Resource
private AppointRecordService appointRecordService;

/**
 * 查询教学楼
 */
@GetMapping("/get-building")
public Result getBuilding(){
   List<String> res =  classroomService.selectBuilding();
   return Result.success(res);
}
/**
 * 获取某个教学楼所有教室
 */
@GetMapping("/get-classroom")
    public Result getClassroom(ClassRoomSearchDto classRoomSearchDto){
    List<Classroom> classroom = classroomService.getClassroom(classRoomSearchDto.getBuilding());
    //查询在这个时间段被预约的教室
    if (classRoomSearchDto.getStartTime()==null||classRoomSearchDto.getEndTime()==null){
        throw new RuntimeException("参数异常");
    }

    //查询该时间段被申请的教室的记录
    Set<Integer> set1 = appointRecordService.selectRoomAndIsAppointByTime(classRoomSearchDto.getStartTime(), classRoomSearchDto.getEndTime())
            .stream().mapToInt(AppointRecord::getRoomId).boxed().collect(Collectors.toSet());
    for (Classroom c : classroom){
        if (set1.contains(c.getId())){
            //说明这个教室在申请中
            c.setClassRoomStatus("1");
        }
    }


    //查询这个时间段被预约的教室的记录并且通过申请了
    List<AppointRecord> roomList =appointRecordService.selectRoomByTime(classRoomSearchDto.getStartTime(), classRoomSearchDto.getEndTime());
    Set<Integer> collect = roomList.stream().map(AppointRecord::getRoomId).collect(Collectors.toSet());
    for (Classroom c : classroom){
      if (collect.contains(c.getId())){
          //说明这个教室被预约了
            c.setClassRoomStatus("2");
      }
    }

    return Result.success(classroom);
}


/**
 * 根据教室id获取教室详情
 */
@GetMapping("/get-classroom-detail")
    public Result getClassroomDetail(Integer roomId){
    Classroom classroom = classroomService.getById(roomId);
    //查询该教室不可预约的时间
   List<String> notAppontTime = classroomService.selectNotAppontTime(roomId);
   classroom.setNotAppontTime(notAppontTime);
    return Result.success(classroom);
}

}

